package net.pessu.android.shoppoid.wishlist;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBar;
import greendroid.widget.ActionBarItem;
import greendroid.widget.NormalActionBarItem;
import net.pessu.android.shoppoid.R;
import net.pessu.android.shoppoid.bean.Article;
import net.pessu.android.shoppoid.gui.IhmObjets;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowArticle extends GDActivity {
	private static final String NON = IhmObjets.getStringContext(R.string.non);
	private static final String OUI = IhmObjets.getStringContext(R.string.oui);
	private static final String ARTICLE_SUPPRIMER = IhmObjets.getStringContext(R.string.articlesupprimer);
	private static final String ETES_VOUS_SUR_DE_VOULOIR_SUPPRIMER_CET_ARTICLE = IhmObjets.getStringContext(R.string.etesvoussursupprimerarticle);

	WishListService service;

	Article article;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setActionBarContentView(R.layout.showarticle);
		ActionBar bar = getActionBar();
		bar.setTitle(getString(R.string.liste));
		bar.setFont(IhmObjets.tfTitre);
		bar.setFontSize(30);
		addActionBarItem(
				getActionBar().newActionBarItem(NormalActionBarItem.class)
						.setDrawable(R.drawable.gd_action_bar_edit)
						.setContentDescription(R.string.edit_article),
				R.id.action_bar_edit_article);
		addActionBarItem(
				getActionBar().newActionBarItem(NormalActionBarItem.class)
				.setDrawable(R.drawable.gd_action_bar_trashcan)
				.setContentDescription(R.string.del_article),
				R.id.action_bar_del_article);

		Bundle b = getIntent().getExtras();

		service = WishListService.getInstance(this);

		article = service.showArticle(b.getInt("id"));

		((TextView) findViewById(R.id.showtitre)).setText(article.getTitre());
		((TextView) findViewById(R.id.showdesc)).setText(article
				.getDescription());
		((TextView) findViewById(R.id.showtype))
				.setText(article.getTypeAchat());
		((TextView) findViewById(R.id.showprice)).setText(IhmObjets.df
				.format(article.getPrixreduit()));
		((TextView) findViewById(R.id.showpriceorigine)).setText(IhmObjets.df
				.format(article.getPrixOrigine()));
		((ImageView) findViewById(R.id.showimg))
				.setBackgroundResource(R.drawable.icon);
		((TextView) findViewById(R.id.showdate)).setText(IhmObjets.sdf
				.format(article.getDate()));
		((Button) findViewById(R.id.showreduc)).setText(IhmObjets.dfpc
				.format((1 - article.getTauxReduc()) * 100) + " %");
		CheckBox checkBoxAcheter = (CheckBox) findViewById(R.id.checkAcheter);
		checkBoxAcheter.setChecked(article.isAcheter());

		checkBoxAcheter
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							service.acheterArticle(article.getId());
						} else {
							service.desAcheterArticle(article.getId());
						}
					}
				});

	}
	

	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {

		switch (item.getItemId()) {
		case R.id.action_bar_edit_article:
			showArticleAdder(article);
			finish();
			break;
		case R.id.action_bar_del_article:
			ajouterBoiteDeDialogue();
			
			break;
		default:
			return super.onHandleActionBarItemClick(item, position);
		}

		return true;
	}
	
	/**
	 * Launches the wishlist activity to add a new contact to the selected
	 * accont.
	 */
	protected void showArticleAdder(Article article) {
		Intent i = new Intent(this, AddArticle.class);
		i.putExtra("article", article);
		startActivity(i);
	}
	

	public void ajouterBoiteDeDialogue() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(ETES_VOUS_SUR_DE_VOULOIR_SUPPRIMER_CET_ARTICLE)
				.setCancelable(false)
				.setPositiveButton(OUI,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								service.delArticle(article);
								if (IhmObjets.listeArticles != null) {
									IhmObjets.listeArticles.refreshAdaptater(service
											.getListArticles());
								} else {
									IhmObjets.isListToRefresh = true;
								}
								finish();
								Toast.makeText(getApplicationContext(), ARTICLE_SUPPRIMER,
										Toast.LENGTH_SHORT).show();
							}
						})
				.setNegativeButton(NON,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}
}
